//
//  LoginViewController.swift
//  麦子洗衣 登录页面
//
//  Created by yangyifan on 15/10/21.
//  Copyright © 2015年 womenshuo. All rights reserved.
//

import UIKit
import SwiftyUserDefaults

class LoginViewController: UIViewController {

    @IBOutlet var phoneText: UITextField!//手机号码文本框
    @IBOutlet var codeText: UITextField!//验证码文本框
    
    let phone   = "15897424254"
    let code    = "0000"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //初始化
        self.initialize()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //初始化
    private func initialize() {
        //设置 登录视图
        self.setView()
    }
    
    //设置 登录视图
    private func setView() {
        
        //文本框 padding-left view
        let phonePaddindLeftView    = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 1))
        let codePaddindLeftView     = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 1))
        
        //设置手机号码文本框
        phoneText.layer.borderWidth = 1.0
        phoneText.layer.borderColor = UIColor.borderColor().CGColor
        phoneText.leftView          = phonePaddindLeftView
        phoneText.leftViewMode      = .Always
        phoneText.placeholder       = "测试手机号码：15897424254"
        
        //设置验证码文本框
        codeText.layer.borderWidth  = 1.0
        codeText.layer.borderColor  = UIColor.borderColor().CGColor
        codeText.leftView           = codePaddindLeftView
        codeText.leftViewMode       = .Always
        codeText.placeholder        = "测试验证码：0000"
    }

    //登录判断
    @IBAction func login(sender: UIButton) {
        
        //逻辑处理 TODO
        
        if (phoneText.text == phone) && (codeText.text == code) {
            //写入 数据 到本地
            self.writeLoginData()
            //跳转到首页
            self.redirectToIndex()
        } else {
            //跳转
            //let alter = UIAlertView(title: "错误", message: "账户或者密码不正确", delegate: nil, cancelButtonTitle: "确认")
            //alter.show()
        }
        
    }
    
    //登录成功 写入 数据 到 本地
    private func writeLoginData() {
        Defaults[.isLogin]  = true
        Defaults[.phone]    = phoneText.text
    }
    
    //跳转到首页
    private func redirectToIndex() {
        self.navigationController?.popToRootViewControllerAnimated(true)
    }
}
